﻿using SqlSugar;
using System.Linq.Expressions;
using Microsoft.Data.SqlClient;
using Hospital.Model.type;
using Hospital.Model.Pay;
using System;
using Hospital.IRepository;
using Hospital.Model.Page;
using Hospital.Model.entity;

namespace Hospital.Repository
{
    /// <summary>
    /// 仓储层实现与数据库交互的基础方法
    /// </summary>
    /// <typeparam name="TEntiry"></typeparam>
    public class BaseRepository<TEntiry> : SimpleClient<TEntiry>, IBaseRepository<TEntiry> where TEntiry : class, new()
    {
        //使用SimpleClient实现对数据库的操作
        public ISqlSugarClient db;
        public BaseRepository(ISqlSugarClient db) : base(db)
        {
            this.db = db;
            //创建数据库,只运行一次
            db.DbMaintenance.CreateDatabase();
            // 手动建多个表
            db.CodeFirst.InitTables(
                typeof(Doctor),
                typeof(Hospitalization),
                typeof(Medication),
                typeof(Medicine),
                typeof(Patient),
                typeof(UserInfo),
                typeof(PatientUser),
                typeof(Specialty),
                typeof(JobTitle),
                typeof(Registration),
                typeof(VisitRecords),
                typeof(Ward),
                typeof(Department),
                typeof(Order),
                typeof(PayRequest),
                typeof(PatientInfo)
            );
        }

        public async Task<bool> CreateAsync(TEntiry entiry)
        {
            return await base.InsertAsync(entiry);
        }

        public async Task<bool> DeleteAsync(int id)
        {
            return await base.DeleteByIdAsync(id);
        }
        public async Task<int> DeleteAsync(List<int> ints)
        {
            return await db.Deleteable<TEntiry>().In(ints).ExecuteCommandAsync();
        }

        public async Task<bool> EditAsync(TEntiry entiry)
        {
            return await base.UpdateAsync(entiry);
        }
        public async Task<bool> EditAsync(Expression<Func<TEntiry, TEntiry>> columns, Expression<Func<TEntiry, bool>> func)
        {
            return await base.UpdateAsync(columns, func);
        }
        public virtual async Task<int> EditAsync(TEntiry entiry, Expression<Func<TEntiry, bool>> func)
        {
            return await db.Updateable(entiry).Where(func).ExecuteCommandAsync();
        }

        public virtual async Task<bool> EditAsync(Expression<Func<TEntiry, bool>> columns, Expression<Func<TEntiry, bool>> func)
        {
            return await db.Updateable<TEntiry>().SetColumns(columns).Where(func).ExecuteCommandAsync() > 0;
        }

        public virtual async Task<TEntiry> FindAsync(int id)
        {
            return await base.GetByIdAsync(id);
        }

        public virtual async Task<List<TEntiry>> QueryAsync()
        {
            return await base.GetListAsync();
        }

        public virtual async Task<List<TEntiry>> QueryAsync(Expression<Func<TEntiry, bool>> func)
        {
            return await base.GetListAsync(func);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public virtual async Task<List<dynamic>> QueryAsync(Expression<Func<TEntiry, dynamic>> expression, Expression<Func<TEntiry, bool>> func)
        {
            return await db.Queryable<TEntiry>().Where(func).Select(expression).ToListAsync();
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="total"></param>
        /// <returns></returns>

        public virtual async Task<List<TEntiry>> QueryAsync(int page, int size, RefAsync<int> total)
        {
            return await base.Context.Queryable<TEntiry>()
                .ToPageListAsync(page, size, total);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageRequest">分页请求</param>
        /// <param name="total">返回的数据量</param>
        /// <returns></returns>
        public virtual async Task<List<TEntiry>> QueryAsync(PageRequest pageRequest, RefAsync<int> total)
        {
            return await base.Context.Queryable<TEntiry>()
                .ToPageListAsync(pageRequest.Page, pageRequest.Size, total);
        }


        /// <summary>
        /// 自定义查询条件的分页查询
        /// </summary>
        /// <param name="func">自定义查询条件</param>
        /// <param name="page">页数</param>
        /// <param name="size">尺寸</param>
        /// <param name="total">总数</param>
        /// <returns></returns>

        public virtual async Task<List<TEntiry>> QueryAsync(Expression<Func<TEntiry, bool>> func, int page, int size, RefAsync<int> total)
        {
            return await base.Context.Queryable<TEntiry>()
                .Where(func)
                .ToPageListAsync(page, size, total);
        }

        public virtual async Task<TEntiry> FindAsync(Expression<Func<TEntiry, bool>> func)
        {
            return await base.GetSingleAsync(func);
        }

        public virtual Task<List<dynamic>> QueryAsyncDynamic()
        {
            throw new NotImplementedException();
        }

    }
}
